﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;

namespace WX.WMS.BLL
{
    public class ClientBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public ClientBLL()
        {
            DAO = new BaseDAO();
        }
        public T_CLIENT GetClientByName(string Name)
        {
            var Clients = DAO.T_CLIENTs.Where(t => t.NAME == Name);
            if (Clients.Count() > 0)
                return Clients.FirstOrDefault();
            else
                return null;
        }
        public T_CLIENT GetClientByID(string ID)
        {
            var Clients = DAO.T_CLIENTs.Where(t => t.ID.ToString() == ID);
            if (Clients.Count() > 0)
                return Clients.FirstOrDefault();
            else
                return null;
        }
        public IEnumerable<T_CLIENT> GetClientList()
        {
            return DAO.T_CLIENTs.ToList();
        }
        public int Add(T_CLIENT Client)
        {
            try
            {
               IEnumerable<T_CLIENT> lastClients = DAO.T_CLIENTs.OrderByDescending(t=>t.ClientCode).ToList();
               if (lastClients == null || lastClients.Count() == 0)
                   Client.ClientCode = "100000";
               else
               {
                  T_CLIENT lastClient = lastClients.FirstOrDefault();
                   Int32 result;
                   if (string.IsNullOrEmpty(lastClient.ClientCode) || !Int32.TryParse(lastClient.ClientCode, out result))
                   {
                       Client.ClientCode = "100000";
                   }
                   else
                   {
                       Client.ClientCode = Convert.ToString(result + 1);
                   }
               }
                Client.ID = DAO.GetGUID();
                DAO.T_CLIENTs.InsertOnSubmit(Client);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int Update(T_CLIENT Client)
        {
            try
            {
                T_CLIENT RawClient = DAO.T_CLIENTs.SingleOrDefault(t => t.ID.ToString() == Client.ID.ToString());
                RawClient.NAME = Client.NAME;
                RawClient.ADDRESS = Client.ADDRESS;
                RawClient.CONTACT = Client.CONTACT;
                RawClient.TEL = Client.TEL;
                DAO.SubmitChanges();
                return 1;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int Delete(T_CLIENT Client)
        {
            try
            {
                T_CLIENT RawClient = DAO.T_CLIENTs.SingleOrDefault(t => t.ID.ToString() == Client.ID.ToString());
                DAO.T_CLIENTs.DeleteOnSubmit(RawClient);
                DAO.SubmitChanges();
                return 1;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public int Delete(string ID)
        {
            try
            {
                T_CLIENT RawClient = DAO.T_CLIENTs.SingleOrDefault(t => t.ID.ToString() ==ID);
                DAO.T_CLIENTs.DeleteOnSubmit(RawClient);
                DAO.SubmitChanges();
                return 1;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public IEnumerable<T_CLIENT> SearchClient()
        {
            return null;
        }
    }
}

